package com.blb.goods_service.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.goods_service.entity.Brand;
import com.blb.goods_service.mapper.BrandMapper;
import com.blb.goods_service.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * BrandService实现类
 */
@CacheConfig(cacheNames = "brand")
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Cacheable(cacheNames = "brand-category",key = "T(String).valueOf(#cid)")
    @Override
    public List<Brand> findBrandsByCategory(Integer cid) {
        return brandMapper.selectBrandsByCategory(cid);
    }

    @Cacheable(key = "T(String).valueOf(#id)")
    @Override
    public Brand findBrandById(Long id) {
        return this.getById(id);
    }

    @CachePut(key = "T(String).valueOf(#brand.id)")
    @Override
    public Brand saveBrand(Brand brand) {
        this.saveOrUpdate(brand);
        return brand;
    }

    @CacheEvict(key = "T(String).valueOf(#id)")
    @Override
    public void deleteBrand(Long id) {
        this.removeById(id);
    }

    @Cacheable(cacheNames = "brand-page",key = "T(String).valueOf(#page)")
    @Override
    public IPage<Brand> pageBrands(Long page) {
        return this.page(new Page<>(page,10));
    }
}
